package cn.ml.sboot11.config;

import cn.ml.sboot11.entity.Student;
import cn.ml.sboot11.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

//@Component
//@Configuration
//@EnableScheduling
public class TestSpringQuartz {

    @Autowired
    private StudentService studentService;

    //每30秒执行一次
    @Scheduled(fixedRate = 1000 * 30)
    public void reportCurrentTime(){
        System.out.println("Scheduling Tasks Examples: Time is now "+ dateFormat().format(new Date()));
    }
    //每10秒执行一次
    @Scheduled(cron = "0/10 * * * * ?")
    public void reportCurrentByCron(){
        System.out.println("Scheduling Tasks Examples By Cron: The time is now " + dateFormat ().format (new Date ()));

        List<Student> students = studentService.list2(null);
        for (Student student:students){
            System.out.println(student);
        }
        System.out.println("--------------------------------------------------------");
    }

    private SimpleDateFormat dateFormat(){
        return new SimpleDateFormat("HH:mm:ss");
    }

}
